/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hackystat.devcathlon.engine.team;

import java.util.List;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 * Test TeamManager DB class.
 * @author Anthony Du
 */
public class TestTeamManager extends DevcathlonTestHelper {
  private static final String teamName = "TestTeam";
  private static final String teamName2 = "TestTeam2";
  private static final String owner = "admin";
  private static final String owner2 = "admin2";
  private static final String domain = "@hackystat.org";
  private static final String user = owner + domain;

  /**
   * Setup team.
   * @throws Exception when an error occurs
   */
  @Before
  public void setUp() throws Exception {
    makeTeam(teamName, owner, "member1", "member2");
  }

  /**
   * Cleanup team(s).
   * @throws Exception when an error occurs
   */
  @After
  public void cleanUp() throws Exception {
    deleteTeam(teamName);
  }

  /**
   * Test CRUD for Teams management.
   * @throws Exception for any errors that may occur.
   */
  @Test
  public void testTeamManager() throws Exception {
    Team team = TeamManager.getInstance().getTeam(teamName);
    assertEquals("should be TestTeam", teamName, team.getName());
    assertEquals("should be owner's email", user, team.getOwner().getEmail());
  }

  /**
   * Test getAllTeams.
   * @throws Exception when an error occurs.
   */
  @Test
  public void testGetAllTeams() throws Exception {
    makeTeam(teamName2, owner2, "member1", "member2");
    List<Team> teams = TeamManager.getInstance().getAllTeams();
    assertFalse("should have teams", teams.isEmpty());
    assertEquals("should have 2 teams", 2, teams.size());
    assertEquals("should be team name: TestTeam", teamName, teams.get(0).getName());
    assertEquals("should be team name: TestTeam2", teamName2, teams.get(1).getName());
    assertEquals("should be 3 TestTeam members", 3, teams.get(0).getMembers().size());
    deleteTeam(teamName2);
  }

  /**
   * Test getTeams given owner's email.
   * @throws Exception when an error occurs.
   */
  @Test
  public void testGetTeams() throws Exception {
    List<Team> teams = TeamManager.getInstance().getTeams(user);
    assertFalse("should have at least one team", teams.isEmpty());
  }
}
